package com.ls.lserp.modules.reportforms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ls.lserp.common.persistence.AjaxDone;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.excel.ExportExcel;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.produce.entity.ProduceWorkticketEntity;
import com.ls.lserp.modules.produce.entity.WorkticketEntity;
import com.ls.lserp.modules.produce.service.ProduceWorkticketService;
import com.ls.lserp.modules.reportforms.entity.ReportformsStyleEntity;
import com.ls.lserp.modules.reportforms.service.ReportformsStyleService;

import freemarker.template.Configuration;

@Controller
@RequestMapping(value = "${adminPath}/reportforms/style/")
public class ReportformsStyleController extends BaseController {
	
    @Autowired
    private ReportformsStyleService reportformsStyleService;
    
    private SXSSFWorkbook sky = new SXSSFWorkbook ();
    
    @Autowired
    Configuration freemarkerConfiguration;


    /**
     * 生产工票一览画面
     *
     * @return
     */
    @RequestMapping(value = {""})
    public String reportformsStyle(Model model) {
    	model.addAttribute("", "");		
        return "modules/reportforms/reportformsStyle";

    }

    /**
     * 拼接一览画面
     *
     * @param values
     * @return
     */
    @RequiresPermissions("reportforms:style:view")
    @RequestMapping(value = {"searchList"})
    @ResponseBody
    public String searchList(ReportformsStyleEntity reportformsStyleEntity, HttpServletRequest request, HttpServletResponse response,
                             Model model, String[] values) throws Exception {
    	
    	List<ReportformsStyleEntity> reportformsList = reportformsStyleService.reportformsStyleInfo(reportformsStyleEntity);
        
        // ajax返回结果
        return renderString (response, reportformsList);
    }
    
    /**
     * 查询是否存在
     *
     * @param values
     * @return
     */
    @RequiresPermissions("reportforms:style:view")
    @RequestMapping(value = {"checkExist"})
    @ResponseBody
    public String checkExist(ReportformsStyleEntity reportformsStyleEntity, HttpServletRequest request, HttpServletResponse response,
                             Model model) throws Exception {
    	
    	List<ReportformsStyleEntity> reportformsList = reportformsStyleService.reportformsStyleInfo(reportformsStyleEntity);
        
        // ajax返回结果
        return renderString (response, reportformsList);
    }
    
    /**
     * 导出生产工票
     *
     * @param response
     * @param produceWorkticketEntity
     * @return
     */
    @RequiresPermissions("reportforms:style:view")
    @RequestMapping(value = "exportReportforms")
    public String exportFile(ReportformsStyleEntity reportformsStyleEntity, HttpServletResponse response) {
        try {
            SXSSFWorkbook wb = new SXSSFWorkbook ();
            List<ReportformsStyleEntity> rEntitys = reportformsStyleService.exportReportforms(reportformsStyleEntity);
            new ExportExcel ("款式报表", wb, "", ReportformsStyleEntity.class, 2).setDataList (rEntitys);
            sky = wb;
            return renderString (response,"true");
        } catch (Exception e) {
            // 记录日志
            logger.error (e.getMessage (), e);
        }

        // 提示错误信息
        return renderString (response, AjaxDone.error (getText ("System_error_please_contact_your_administrator")));

    }
    /**
     * Excel下载
     *
     * @param response
     * @return
     * @throws Exception
     */
    @RequiresPermissions("reportforms:style:view")
    @RequestMapping(value = {"makeExportFile"})
    public void makeExportFile(ReportformsStyleEntity reportformsStyleEntity, HttpServletResponse response, HttpServletRequest request)
            throws Exception {
    	String fileName = DateUtils.getDate("yyyyMMdd") + "款式报表(" + reportformsStyleEntity.getStartDate().replace("-", "") + "~" + reportformsStyleEntity.getEndDate().replace("-", "") + ").xlsx";
        response.reset ();
        response.setContentType ("application/octet-stream; charset=utf-8");
        response.setCharacterEncoding ("UTF-8");
        response.setHeader ("Content-Disposition", "attachment; filename=" + new String (fileName.getBytes (), "ISO-8859-1"));
        sky.write (response.getOutputStream ());
    }

}
